--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--开服首次沙比克攻城战


--开服活动的占领行会
function SendOpenServerGuildSiegeData(sysarg)
	local ssVar = System.getStaticVar()
    local guildName = ssVar.openServerGuildName
	if not guildName then
		guildName = ""
	end
	local npack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerFirstSeige)
	if npack  then
		DataPack.writeString(npack,guildName)
		DataPack.flush(npack)
	end
end


--开服活动的沙城战
function OnOpenServerGuildSiegeOverHandle()
	local day = System.getDaysSinceOpenServer()
	local GuildSiegCfg = OpenServerCfg.GuildSiege
	if day < GuildSiegCfg.day  then
		return
	end

	local ssVar = System.getStaticVar()
    local guildName = ssVar.openServerGuildName
	if guildName and guildName ~= "" then
		return 
	end

	--攻城占领行会
	local nCityOwnGuildId 	= System.getSbkGuildId()
	local pGuild 			= Guild.getGuild(nCityOwnGuildId)
	if nCityOwnGuildId > 0 and pGuild then
		local guildName 	= Guild.getGuildName(nCityOwnGuildId)
		local memberList 	= Guild.getGuildMemberIdList(pGuild) or {}
		local leaderId 		= Guild.getGuildLeaderId(nCityOwnGuildId)
		local title 		= Lang.ScriptTips.OpenServer010
		local content1 		= Lang.ScriptTips.OpenServer011
		local content2 		= Lang.ScriptTips.OpenServer012
		local logId 		= 1096
		local logStr 		= Lang.LogTips.OpenServerFirstSeigeWar

		for k,v in pairs(memberList) do
			local sex,job = Guild.getGuildMemberBasicData(pGuild, v, 0, 0)	--获取此成员的sex和job（成员可能不在线）
			if v == leaderId then --首领
				local awards  = FilterCommonAwardsByCond(GuildSiegCfg.leaderAwards, sex, job )     --过滤职业性别
				if #awards ~= 0 then
					Awards.GiveToMailById(v, awards, title, content1, logId, logStr)
				end
			else
				local awards  = FilterCommonAwardsByCond(GuildSiegCfg.memberAwards, sex, job )     --过滤职业性别
				if #awards ~= 0 then
					Awards.GiveToMailById(v, awards, title, content2, logId, logStr)
				end
			end
		end

		if guildName then
			local ssVar = System.getStaticVar()
			ssVar.openServerGuildName = guildName
			local msg = string.format(Lang.ScriptTips.OpenServer013, guildName)
			System.broadcastTipmsg(msg, ttChatWindow)
		end
	end
end

-----------------------------开服行会----------------------------------------------

--[开服活动]请求开服活动数据
function OpenServerGuildInitData(sysarg, packet)
	if not IsInOpenServerDay() then
		return
	end
	OpenServerGuildInitDataImp(sysarg)
end

--领取开服活动奖励
function OpenServerGuildGetAward(sysarg, packet)                                                                       
	local nIndex = DataPack.readByte( packet )
	local actorId = Actor.getActorId(sysarg)
	local actCfg, myData, sHadSendCountVar = OpenServerGetGuildCfgAndVarById(actorId)

	if not actCfg then
		return
	end
	
	if not IsInOpenServerDay() then
		return
	end

	local nOpenServerDay = System.getDaysSinceOpenServer()
	if actCfg.openDay and nOpenServerDay < actCfg.openDay then --未到开启天数，不能领奖励	
		return
	end

	if actCfg.endDay and nOpenServerDay > actCfg.endDay then --超过开启天数，不能领奖励	
		return
	end

	local subAwards = actCfg.Awards[nIndex]
	if  not subAwards  then
		return
	end
	if not myData or myData == "" or myData == 0 then
		return
	end
	if not myData.aAwardVar then
		myData.aAwardVar = {}
	end
	local hadSendCount = sHadSendCountVar[nIndex] or 0
	if subAwards.maxCount == -1 or hadSendCount < subAwards.maxCount then --可以领
		local awardState = myData.aAwardVar[nIndex] or OpenServerAwardState_No_Complete
		if awardState == OpenServerAwardState_Complete_No_Award then
			if not Awards.CheckBagGridCount(sysarg, subAwards.awards) then
				Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
				return
			end
			Awards.Give(sysarg,subAwards.awards,1096,Lang.LogTips.OpenServerGuildLog)
			sHadSendCountVar[nIndex] = hadSendCount + 1
			myData.aAwardVar[nIndex] = OpenServerAwardState_Complete_Had_Award
			
			local newData = BaseFuc_serialize(myData)
			gRankMgr_SetRankEx(actorId, RANK_OPENSERVER_GUILD, 1, newData)

			OpenServerGuildInitDataImp(sysarg)
		end
	elseif hadSendCount >= subAwards.maxCount then
		OpenServerGuildInitDataImp(sysarg)
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServer006, ttFlyTip)
		return
	end

end
--根据id获取配置或者变量
function OpenServerGetGuildCfgAndVarById(actorId)
	local sVar = System.getStaticVar()
	if not sVar.openserverGuildSendAwardCount then
		sVar.openserverGuildSendAwardCount = {}
	end

	local GuildCfg = OpenServerCfg.Guild    

	local data = gRankMgr_GetValueById(actorId, RANK_OPENSERVER_GUILD, 1)
	local myData = BaseFuc_unserialize(data)
	
	
	return GuildCfg, myData, sVar.openserverGuildSendAwardCount
end

function OnScriptGuildOperation( guildType, guildLeaderId, param )
	--print("guildType ="..guildType)
	--print("guildLeaderId ="..guildLeaderId)
	--print("param ="..param)
	OpenServerGuildTriggleEvent(guildLeaderId, guildType, param)
end


function OpenServerGuildTriggleEvent(actorId, eventId, param) 
	if not eventId or not param or not actorId then
		return
	end
	if eventId <= enScriptGuildType_No or eventId >= enScriptGuildType_Max then
		return
	end
	if param == 0 then
		param = 1
	end
	local actCfg, myData, sHadSendCountVar = OpenServerGetGuildCfgAndVarById(actorId)
	--print("enScriptGuildType_Create ="..enScriptGuildType_Create)
	--print("eventId ="..eventId)
	if eventId ~= enScriptGuildType_Create and (not myData or myData == "" or myData == 0 ) then
		return
	end
	if not myData then
		myData = {}
	end
	if not myData.osGuild then
		myData.osGuild = {}
	end
	if not myData.osGuild[eventId] then
		myData.osGuild[eventId] = 0
	end
	if eventId == enScriptGuildType_Settle then
		myData.osGuild[eventId] = myData.osGuild[eventId] + param
	else
		myData.osGuild[eventId] = param
	end

	OnOpenServerGuildHandler(actorId, eventId, myData, actCfg)
end
	
--六道
function OnOpenServerGuildHandler(actorId,eventId, myData, actCfg)
	if not IsInOpenServerDay() then
		return
	end

	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay
	local nOpenServerDay = System.getDaysSinceOpenServer()

	if nOpenServerDay > actEndDay then
		return
	end

	local param = myData.osGuild[eventId]

	if not myData.aAwardVar then
		myData.aAwardVar = {}
	end

	local sendData = false
	for idx, cfg in ipairs(actCfg.Awards) do
		if cfg.nGuild == eventId then
			local awardState = myData.aAwardVar[idx] or OpenServerAwardState_No_Complete
			if cfg.nNeedCount and param >= cfg.nNeedCount then
				if awardState == OpenServerAwardState_No_Complete then --还未完成
					myData.aAwardVar[idx] = OpenServerAwardState_Complete_No_Award
					if nOpenServerDay >= actOpenDay then
						sendData = true
					end
				end
			elseif awardState == OpenServerAwardState_No_Complete and nOpenServerDay >= actOpenDay then
				sendData = true
			end
		end
	end

	if sendData then
		local newData = BaseFuc_serialize(myData)
		gRankMgr_SetRankEx(actorId, RANK_OPENSERVER_GUILD, 1, newData)

		local sysarg = Actor.getActorById(actorId)
		if sysarg then
			OpenServerGuildInitDataImp(sysarg)
		end
		
	end
end


--[开服活动]通用界面数据(冲级，龙珠，兵魂，翅膀,坐骑)
--nId  活动id
function OpenServerGuildInitDataImp(sysarg)
	local actorId = Actor.getActorId(sysarg)
    local actCfg, myData, sHadSendCountVar = OpenServerGetGuildCfgAndVarById(actorId)
	if not actCfg then
		return
	end
	local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sOpenServerGuildInitData)
	if npack == nil then 
		return 
	end
	local nOpenServerDay = System.getDaysSinceOpenServer()

	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay

	DataPack.writeByte(npack, #actCfg.Awards)
	if not myData then
		myData = {}
	end
	if not myData.osGuild then
		myData.osGuild = {}
	end
	if not myData.aAwardVar then
		myData.aAwardVar = {}
	end
	for k,v in pairs(actCfg.Awards) do
		--DataPack.writeByte(npack, k)
		--print("k ="..k)
		local awardState = myData.aAwardVar[k] or OpenServerAwardState_No_Complete
		--print("v.max = "..v.maxCount)
		--print("sHadSendCountVar[k] ="..(sHadSendCountVar[k] or 0))
		local leftCount = v.maxCount - (sHadSendCountVar[k] or 0)
		if v.maxCount == -1 then
			leftCount = -1
		end
		--print("leftCount ="..leftCount)
		local currentValue = myData.osGuild[actCfg.Awards[k].nGuild] or 0

		if  nOpenServerDay >= actOpenDay and nOpenServerDay <= actEndDay then
			--print("awardState ="..awardState)
			--print("leftCount ="..leftCount)
			--print("currentValue ="..currentValue)
			DataPack.writeByte(npack, awardState)
			DataPack.writeShort(npack, leftCount)
			DataPack.writeShort(npack, currentValue)
		else
			--未到开放时间
			--print("awardState =0")
			--print("leftCount =0")
			--print("currentValue =0")
			DataPack.writeByte(npack, 0)
			DataPack.writeWord(npack, 0)
			DataPack.writeShort(npack, 0)
		end
	end
	DataPack.flush(npack) 
end




